Navigation

  • index
  • next |
  • previous |
  • PyHowTo documentation »
  • Basic »
  • Math »

Table of Contents

Python v3.7 HowTos:

  • ----------------
  • Recursion
  • Backtracking
  • Dynamic Programming
  • Greedy
  • Sort
  • Binary Search
  • Depth First Search [DFS]
  • Breadth First Search [BFS]
  • Binary Search Tree [BST]
  • ----------------
  • Array
  • String
  • Heap
  • Stack
  • Queue
  • Tree
  • Linked List
  • Hash Table
  • Bit Manipulation
  • Two Pointers
  • Math
  • Decorator
  • ----------------
  • Basic
  • Intermediate
  • Advanced
  • Interview
  • ----------------
  • Spark
  • Tkinter
  • Turtle
  • Games
  • Web
  • ----------------
  • About
  • History

Previous topic

Flip a coin 1000 times and count heads and tails

Next topic

Randomly select an item from a list

Quick search

Print a random sample of words from the system dictionary¶

Print a random sample of words from the system dictionary.
Expected output:
cellophane’s
matter’s
Whiteley’s
airdrop’s
sulkiest
whisper’s
downturns
import random

with open('/usr/share/dict/words', 'rt') as fh:
    words = fh.readlines()

words = [w.rstrip() for w in words]

for w in random.sample(words, 7):
    print(w)

Output:

philatelist's
thieve
sparrows
Loretta's
hostiles
naughty
afflicts

See also

https://www.w3resource.com/python-exercises/math/python-math-exercise-54.php

Navigation

  • index
  • next |
  • previous |
  • PyHowTo documentation »
  • Basic »
  • Math »
© Copyright 2020, Sergiy Zaytsev, szaytsev@hotmail.com. Created using Sphinx 2.3.0.